____ _ _ _ _
| _ \ ___ | |_ (_) _ __ ___ __| | (_) __ _
| |_) | / _ \ | __| | | | '_ \ / _ \ / _| | | | / _ |
| _ < | __/ | |_ | | | |_) | | __/ | (_| | | | | (_| |
|_| \_\ \___| \__| |_| | .__/ \___| \__,_| |_| \__,_|
|_|
- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b- `b
Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―Β―
Value Object
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
top
Das Value Object (auch Wertobjekt) ist ein in der Softwareentwicklung eingesetztes Entwurfsmuster. Wertobjekte sind unverΓ€nderbare Objekte, die einen speziellen Wert reprΓ€sentieren. Soll der Wert geΓ€ndert werden, so muss ein neues Objekt generiert werden.cite-ref-1[1] Das Entwurfsmuster wird dazu genutzt, Objektvergleiche auf deren Attributwerte zu beziehen, anstatt auf ObjektidentitΓ€t.cite-ref-2[2]
Contents
β’ ErklΓ€rung
β’ Beispiele
β’ Datum
β’ Beispiel (Java)
β’ Weblinks
β’ Einzelnachweise
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ErklΓ€rung
FΓΌr spezielle Werte kann es sinnvoll sein, diese als Objekte einer eigenen Klasse zu verwenden. HΓ€ufig verwendet wird dieses Entwurfsmuster beispielsweise fΓΌr GeldbetrΓ€ge oder Datumsangaben. Bei diesen komplexeren Datentypen kΓΆnnen den jeweiligen Klassen von Beginn an eigene Methoden und Eigenschaften mitgegeben werden, die das spΓ€tere Arbeiten mit dem Wertobjekt vereinfachen.
Wertobjekte haben, nach der Definition von Eric Evanscite-ref-3[3], drei fundamentale Eigenschaften:
β’ Wertobjekte haben keine IdentitΓ€t
β’ Wertobjekte sind nicht verΓ€nderbar
β’ Wertobjekte werden immer in einem gΓΌltigen Zustand erzeugt
Beispiele
Datum
Die Klasse "Datum" besitzt die Methoden "getYear()" und "getEuropeanDate()" β diese erleichtern den Umgang mit Objekten vom Typ "Datum".
Beispiel (Java)
β’ Wertobjekt-Klasse Money (Geld)
import java.math.BigDecimal;
import java.util.Currency;
import java.util.Objects;
public final class Money {
private final BigDecimal amount;
private final Currency currency;
public Money(BigDecimal amount, Currency currency) {
Objects.requireNonNull(amount, "amount should be not null");
Objects.requireNonNull(currency, "currency should be not null");
this.amount = amount;
this.currency = currency;
}
public BigDecimal getAmount() {
return this.amount;
}
public Currency getCurrency() {
return this.currency;
}
public Money add(Money other) {
if (other == null || other.currency != currency) throw new IllegalArgumentException("Can't add " + other);
return new Money(amount.add(other.amount), currency);
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass())
return false;
Money other = (Money) obj;
return amount.equals(other.amount) && currency.equals(other.currency);
}
}
Weblinks
β’ ErklΓ€rung mit Beispielen (PDF; englisch)
β’ ErklΓ€rung mit Beispielen (deutsch)
Einzelnachweise
cite-note-11. β 4.4 Klassen von Werten und Klassen von Objekten. Rheinwerk Computing β Praxisbuch Objektorientierung; abgerufen am 31. August 2015
cite-note-22. β Karl Eilebrecht, Gernot Starke: Patterns kompakt. 4. Auflage. Springer Vieweg Verlag, Berlin 2013, ISBN 978-3-642-34717-7, S. 195β197.
cite-note-33. β heise.de - Einhaltung von Invarianten mit dem Value Object Pattern Abgerufen am 28. MΓ€rz 2016.